// 点击扩展的图标截图
// https://developer.chrome.com/docs/extensions/reference/api/action?hl=zh-cn
chrome.action.onClicked.addListener(async function () {
    // https://developer.chrome.com/docs/extensions/reference/api/tabs?hl=zh-cn
    const screenshotUrl = await chrome.tabs.captureVisibleTab(); // 截取指定窗口中当前处于活动状态的标签页的显示区域
    console.log(screenshotUrl);

    // https://developer.chrome.com/docs/extensions/reference/api/downloads?hl=zh-cn#method-download
    // https://github.com/GoogleChrome/chrome-extensions-samples/blob/main/_archive/mv2/api/downloads/download_links/manifest.json
    chrome.downloads.download(
        {
            // url、文件名、是否另存为
            url: screenshotUrl,
            filename: "screenshot-by-click",
            saveAs: true
        },
    );
});

// 使用快捷键截图
// https://developer.chrome.com/docs/extensions/reference/api/commands?hl=zh-cn
chrome.commands.onCommand.addListener(async function (command) {
    if (command == "shortcut") {
        // https://developer.chrome.com/docs/extensions/reference/api/tabs?hl=zh-cn
        const screenshotUrl = await chrome.tabs.captureVisibleTab(); // 截取指定窗口中当前处于活动状态的标签页的显示区域
        console.log(screenshotUrl);

        // https://developer.chrome.com/docs/extensions/reference/api/downloads?hl=zh-cn#method-download
        // https://github.com/GoogleChrome/chrome-extensions-samples/blob/main/_archive/mv2/api/downloads/download_links/manifest.json
        chrome.downloads.download(
            {
                // url、文件名、是否另存为
                url: screenshotUrl,
                filename: "screenshot-by-command",
                saveAs: true
            },
        );
    }
});